/**
 * 
 */
package vue;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import vue.popup.IGImportSolverDialog;
import vue.popup.IGRemoveImportSolverDialog;
import controller.IController;

/**
 * 
 * A {@link JMenuBar} for the application GUI.
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class IGSATJMenuBar extends JMenuBar implements KeyListener {

	private static final long	serialVersionUID		= 1L;

	public static final String	FILE					= "File";
	public static final String	SOLVER					= "Solver";
	public static final String	HELP					= "Help";

	public static final String	NEW_SESSION				= "Open CNF";
	public static final String	LOAD_SESSION			= "Import session";
	public static final String	CLOSE					= "Close application";
	public static final String	EXPORT_XML				= "Export current session";

	public static final String	ADD_SOLVER				= "Import solver";
	public static final String	REMOVE_SOLVER			= "Remove imported solver";

	public static final String	SAVE_SOLVER				= "Save imported solver";


	public static final String	ABOUT					= "About";


	private IController			controller				= null;
	private IIGExtended			ig						= null;

	private JMenu				file					= null;
	private JMenu				solver					= null;
	private JMenu				help					= null;

	// private File lastDirectory = new File("tstcnf");
	private File				lastDirectoryCNF		= new File(System.getProperty("user.dir") + "/tstcnf/");
	private File				lastDirectorySession	= new File(System.getProperty("user.dir") + "/save/");


	/**
	 * Create an {@link IGSATJMenuBar}.
	 * 
	 * @param ig
	 *            the ig
	 */
	public IGSATJMenuBar(IIGExtended ig) {
		this.ig = ig;
		createFileMenu();
		createSolverMenu();
		createHelpMenu();

		addKeyListener(this);
		setFocusable(true);
	}


	/**
	 * Return the controller of the menu bar.
	 * 
	 * @return the controller.
	 */
	public IController getController() {
		return controller;
	}


	/**
	 * Set the controller of the menu bar.
	 * 
	 * @param controller
	 *            the controller to set
	 */
	public void setController(IController controller) {
		this.controller = controller;
	}

	/**
	 * Create the menu File.
	 */
	public void createFileMenu() {
		file = new JMenu(FILE);

		// item nouvelle cnf
		JMenuItem newcnf = new JMenuItem(NEW_SESSION);
		newcnf.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// lastDirectory = new
				// File(IGSATJMenuBar.class.getClass().getResource("tstcnf").getFile());

				/*
				 * System.out.println("existe " + lastDirectory.exists() + " " +
				 * lastDirectory.getAbsolutePath() + "  " + lastDirectory.getPath());
				 * 
				 * 
				 * JOptionPane.showMessageDialog(null, "existe " + lastDirectory.exists() + " " +
				 * lastDirectory.getAbsolutePath() + "  " + lastDirectory.getPath(), "ze",
				 * JOptionPane.ERROR_MESSAGE);
				 */
				JFileChooser fc = new JFileChooser(lastDirectoryCNF.getAbsoluteFile());
				int returnVal = fc.showOpenDialog(null);

				if (returnVal == JFileChooser.APPROVE_OPTION)
				{
					File file = fc.getSelectedFile();
					lastDirectoryCNF = new File(file.getParent());
					controller.openSession(file);
				}

				/*
				 * System.out.println("existe " + lastDirectory.exists() + " " +
				 * lastDirectory.getAbsolutePath() + "  " + lastDirectory.getPath());
				 * 
				 * 
				 * JOptionPane.showMessageDialog(null, "existe " + lastDirectory.exists() + " " +
				 * lastDirectory.getAbsolutePath() + "  " + lastDirectory.getPath(), "ze",
				 * JOptionPane.ERROR_MESSAGE);
				 */
			};
		});

		// item charger session
		JMenuItem loadcnf = new JMenuItem(LOAD_SESSION);
		loadcnf.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				JFileChooser fc = new JFileChooser(lastDirectorySession.getAbsoluteFile());
				fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				int returnVal = fc.showOpenDialog(null);

				if (returnVal == JFileChooser.APPROVE_OPTION)
				{
					File file = fc.getSelectedFile();
					lastDirectorySession = new File(file.getParent());
					controller.loadSession(file);
				}

			};
		});

		// item export xml
		JMenuItem exportXML = new JMenuItem(EXPORT_XML);
		exportXML.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ig.lunchExportXML();
			}
		});

		// item quitter
		JMenuItem close = new JMenuItem(CLOSE);
		close.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				controller.close();
			}
		});

		// finalise et ajoute le menu dans la barre
		file.add(newcnf);
		file.addSeparator();
		file.add(loadcnf);
		file.add(exportXML);
		file.addSeparator();
		file.add(close);

		add(file);
	}

	/**
	 * Create the solver File.
	 */
	public void createSolverMenu() {
		solver = new JMenu(SOLVER);

		// item import solver
		JMenuItem importSolver = new JMenuItem(ADD_SOLVER);
		importSolver.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				IGImportSolverDialog dialog = new IGImportSolverDialog(controller, null);
				dialog.setVisible(true);
			}
		});

		// item remove solver
		JMenuItem removeSolver = new JMenuItem(REMOVE_SOLVER);
		removeSolver.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				IGRemoveImportSolverDialog dialog = new IGRemoveImportSolverDialog(controller, null);
				dialog.setVisible(true);
			}
		});

		// item save solver
		JMenuItem saveSolver = new JMenuItem(SAVE_SOLVER);
		saveSolver.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				controller.saveImportedSolver();
			}
		});

		// finalise et ajoute le menu dans la barre
		solver.add(importSolver);
		solver.add(removeSolver);
		solver.addSeparator();
		solver.add(saveSolver);

		add(solver);
	}

	/**
	 * Create the menu Help.
	 */
	public void createHelpMenu() {
		help = new JMenu(HELP);

		// item about
		JMenuItem about = new JMenuItem(ABOUT);
		about.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				new AboutDialog().setVisible(true);
			}

		});

		help.add(about);
		add(help);
	}


	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_F1)
			help.doClick();
	}


	@Override
	public void keyReleased(KeyEvent e) {

	}


	@Override
	public void keyTyped(KeyEvent e) {

	}

}
